//
//  YGStatusCell.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/16.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGStatusCell: UITableViewCell {
    
    /// 原创模型
    var status: YGStatus? {
        didSet {
            // 传递给原创view
            originalView.status = status
            // 传递给被转发的view
            retweetView.retweetStatus = status?.retweeted_status
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置UI
        prepareUI()
    }
    
    private func prepareUI() {
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        // 原创view
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(10)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
            // 测试的高度
//            let height = Int(arc4random_uniform(100)) + 50
//            make.height.equalTo(height)
        }
        
        // 被转发
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
//            make.height.equalTo(55)
        }
        
        // 底部view
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(retweetView.snp_bottom)
            make.trailing.equalTo(contentView)
            make.height.equalTo(37)
        }
        
        // 根据Cell的约束自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            
            // 底部
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    /// 原创view
    private lazy var originalView: YGStatusOriginalView = YGStatusOriginalView()
    
    /// 被转发view
    private lazy var retweetView: YGStatusRetweetView = YGStatusRetweetView()
    
    // 底部view
    private lazy var bottomView: YGStatusBottomView = YGStatusBottomView()
    
}
